package com.example.rg1moviemobileapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private Retrofit retrofit;
    private RequestMovie requestMovie;
    private List<Movie> movieList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.listView);
        createRetrofit();
        initListView();


    }

    private void createRetrofit()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl("http://172.28.22.100:8088/movie/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        requestMovie=retrofit.create(RequestMovie.class);
    }

    private void initListView()
    {
        Call<List<Movie>> call=requestMovie.getMovieList();
        call.enqueue(new Callback<List<Movie>>() {
            @Override
            public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
                movieList=response.body();
                List<String> titleList=new ArrayList<>();
                for(Movie movie:movieList)
                {
                    titleList.add(movie.getTitle());
                }
                ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_list_item_1,titleList);
                listView.setAdapter(arrayAdapter);
            }

            @Override
            public void onFailure(Call<List<Movie>> call, Throwable t) {

            }
        });
    }
}
